package com.open.capacity.reimburse.service.impl;

import com.open.capacity.model.system.SysUser;
import com.open.capacity.reimburse.dao.BasePayItemDao;
import com.open.capacity.reimburse.entity.BasePayItem;
import com.open.capacity.reimburse.entity.BasePayItemForm;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.SysUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.*;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.BasePayItemAttach;
import com.open.capacity.reimburse.dao.BasePayItemAttachDao;
import com.open.capacity.reimburse.service.BasePayItemAttachService;


@Service
public class BasePayItemAttachServiceImpl implements BasePayItemAttachService {

    @Autowired
    private BasePayItemDao basePayItemDao;
    @Autowired
    private BasePayItemAttachDao basePayItemAttachDao;

    /**
     * 添加
     *
     * @param basePayItemAttach
     */
    public int save(BasePayItemAttach basePayItemAttach) {
        SysUser currentUser = SysUserUtil.getLoginAppUser();

        basePayItemAttach.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        basePayItemAttach.setUpdateUserid(currentUser.getId());
        basePayItemAttach.setUpdateUsername(currentUser.getRealName());
        basePayItemAttach.setUpdateTime(new Date());
        return basePayItemAttachDao.save(basePayItemAttach);
    }

    /**
     * 修改
     *
     * @param basePayItemAttach
     */
    public int update(BasePayItemAttach basePayItemAttach) {
        SysUser currentUser = SysUserUtil.getLoginAppUser();
        basePayItemAttach.setUpdateUserid(currentUser.getId());
        basePayItemAttach.setUpdateUsername(currentUser.getRealName());
        basePayItemAttach.setUpdateTime(new Date());
        return basePayItemAttachDao.update(basePayItemAttach);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return basePayItemAttachDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public BasePayItemAttach findById(String id) {
        return basePayItemAttachDao.findById(id);
    }

    @Override
    public List<BasePayItemAttach> findByPayitemId(String payitemId, String category) {
        Map<String, Object> params = new HashMap<>();
        params.put("payitemId", payitemId);
        params.put("category", category);
        return basePayItemAttachDao.findAll(params);
    }

    @Override
    public List<BasePayItemAttach> findByPayitemIds(String payitemIds, String category) {
        String[] idArrays = payitemIds.split(",");

        List<BasePayItemAttach> list = new ArrayList<>();
        Map<String, Object> params;
        for (String id : idArrays) {
            BasePayItem payitem = basePayItemDao.findById(id);
            params = new HashMap<>();
            params.put("payitemId", id);
            params.put("category", category);
            List<BasePayItemAttach> attaches = basePayItemAttachDao.findAll(params);
            list.addAll(attaches);
        }
        return list;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<BasePayItemAttach> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<BasePayItemAttach> list = basePayItemAttachDao.findAll(params);
        PageInfo<BasePayItemAttach> pageInfo = new PageInfo(list);

        return PageResult.<BasePayItemAttach>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<BasePayItemAttach> findAll(Map<String, Object> params) {
        return basePayItemAttachDao.findAll(params);
    }

}
